var codeUuid;
var times=0;
$(function(){
	$(".l_inputName").focus(function(){
		$(this).addClass("l_insetbg");
	});
	$(".l_inputName").blur(function(){
		$(this).removeClass("l_insetbg");
	});
	$(".g_addIn").hover(function(){
		$(this).css("background-color","#6793f6");
	},function(){
		$(this).css("background-color","#4e7add");
	})
	
	
	$("#errorMsg").css('top','280px');
	$("#username").blur(function(){
		$(this).css('color','#222');
	});
	$("#username").focus(function(){
		$(this).css('color','#222');
	});
	$("#userpasswd").blur(function(){
		$(this).css('color','#222');
	});
	$("#userpasswd").focus(function(){
		$(this).css('color','#222');
	});
});


function refreshCaptcha() {
        codeUuid=uuid();
		$.ajax({
		type : "get",
		url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
		dataType:'json',
		jsonp:'callback',
		async : false,
		success : function(result) {
			if (result.retCode == '0000') {
				data=result.data;
				$('#captchaImage').attr('src',server_url+data);
			}
			else{
				data="";
			}
		}
	});
}
//回车
	document.onkeydown = function (event){ 
		if (event.keyCode==13) //回车键的键值为13 
			submitLogin();
		};
	
	$("#submitButton").click(function(){
		submitLogin();
	});
	
	function submitLogin() {
		times+=1;
		
		var username = $('#username').val();
		var userpasswd = $('#userpasswd').val();
		var yzm = $('#yzm').val();
		if(username==''){
			$('#errorMsg').text("请输入用户名！");
			times-=1;
			return false;
		}
		if(userpasswd==''){
			times-=1;
			$('#errorMsg').text("请输入密码！");
			return false;
		}
		
		if(times>3){
			if(yzm==''){
				times-=1;
				$('#errorMsg').text("请输入验证码！");
				return false;
			}
		}
		
	   
	   $.ajax({
		type : "post",
		url : server_url+'rest/login.do?username='+username+'&userpasswd='+userpasswd+'&codeUuid='+codeUuid+'&code='+yzm+'&times='+times,
		dataType:'json',
		async : false,
		success : function(result) {
			if(times>=3){
				$("#yzm").css('display','inline');
				$("#c_Image").css('display','inline');
				$("#errorMsg").css('top','350px');
				getcodeUuid();
			}
			
//		    console.debug(result);
			if (result.retCode == '0000') {
				token=result.data.token;
				username=result.data.username;
				userid=result.data.userid;
				$("#token").attr('value',token);
				
//				console.debug($("#token"));
				if(localStorage){
					localStorage.setItem("token",token);
					localStorage.setItem("username",username);
					localStorage.setItem("userid",userid);
				}
				
				var url='index.do';
				window.location.href="index.do?ran="+Math.random();
//				window.location.href="index.do";
//				return true;
			}
			else if(result.retCode == '1004')
			{
				$('#errorMsg').text("验证码错误！");
				return false;
			}
			else if(result.retCode == '1005')
			{
				$('#errorMsg').text("用户名或者密码错误！");
				return false;
			}
			else if(result.retCode == '1002'){
				$('#errorMsg').text("用户名或者密码错误！");
				return false;
			}
			else if(result.retCode == '1003'){
				$('#errorMsg').text("用户名没有权限！");
				return false;
			}
		 },
		 error: function(){
			 $('#errorMsg').text("服务器异常");
			 if(times>=3){
					$("#yzm").css('display','inline');
					$("#c_Image").css('display','inline');
					$("#errorMsg").css('top','294px');
					getcodeUuid();
				}
		 }
	   
	   });
	   
		
	}
	
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}	

function getcodeUuid(){
	codeUuid=uuid();			
	$.ajax({
			type : "get",
			url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
			dataType:'json',
			jsonp:'callback',
			async : false,
			success : function(result) {
				if (result.retCode == '0000') {
					data=result.data;
					$('#captchaImage').attr('src',server_url+data);
				}
				else{
					data="";
				}
			}
		});
}